package com.ts.system.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ts.comm.SysResult;
import com.ts.file.ExcelUtil;
import com.ts.system.dao.SystemDao;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;
/**
 * Created by 马俊鹏 on 2017/4/10.
 */
@Controller
public class SystemController{
	@Resource
	private SystemDao systemDao;

	//获取机构的list
	@ResponseBody
	@RequestMapping(value = "/getDepartmentList")
	public SysResult getDepartmentList() throws Exception {
		List<Map<String,Object>> departmentList = null;
		SysResult sysResult = new SysResult();
		try {
			departmentList = systemDao.getDepartmentList();
			sysResult.setRows(departmentList);
		} catch (Exception e) {
			sysResult.setMsg(e.getMessage());
//			throw new Exception("获取机构列表失败",e);
		}
		return sysResult;
	}

	//添加机构
	@ResponseBody
	@RequestMapping(value = "/addDepartment",produces = {"text/html;charset=UTF-8","application/json"})
	public String addDepartment(HttpServletRequest request, HttpServletResponse response,@RequestBody  String code){
		JSONObject object = JSONObject.parseObject(code);
		return "哈哈";
	}

	//删除机构
	@ResponseBody
	@RequestMapping(value = "/deleteDepartment",produces = {"text/html;charset=UTF-8","application/json"})
	public SysResult deleteDepartment(HttpServletRequest request, HttpServletResponse response, @RequestBody  String code) throws Exception {
		JSONArray array = JSON.parseArray(code);
		Boolean flag = true;
		SysResult sysResult = new SysResult();
		for(Object fcodeObject : array){
			String fcode = (String)fcodeObject;
			flag = systemDao.deleteDepartment(fcode);
		}
		if(flag)
			sysResult.setMsg("success");
		else
			sysResult.setMsg("error");
		return sysResult;
	}

	//获取所属机构列表
	@ResponseBody
	@RequestMapping(value = "/getParentDepartment" ,produces = {"text/json;charset=UTF-8"})
	public String getParentDepartment() throws Exception{
		String resultJsonStr =  null;
		resultJsonStr = systemDao.getParentDepartmentList();
		return resultJsonStr;
	}

	@RequestMapping("/exportExcel")
	public String exportExcel(HttpServletRequest request,HttpServletResponse response) throws IOException{

		String fileData = request.getParameter("fileData");
//        String value= new String(fileData.getBytes("ISO8859-1"),"UTF-8");
		HSSFWorkbook wb = ExcelUtil.exportExcel(fileData);
		String fileName="excel文件";
		//填充projects数据
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try {
			wb.write(os);
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] content = os.toByteArray();
		InputStream is = new ByteArrayInputStream(content);
		// 设置response参数，可以打开下载页面
		response.reset();
		response.setContentType("application/force-download");
		response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
		ServletOutputStream out = response.getOutputStream();
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(is);
			bos = new BufferedOutputStream(out);
			byte[] buff = new byte[2048];
			int bytesRead;
			// Simple read/write loop.
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (final IOException e) {
			throw e;
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
		return null;
	}
}
